﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace FancyBattles
{
    static class InputHandler
    {
        #region Atributes

        private static Dictionary<PlayerIndex, GamePadState> lastInputs = new Dictionary<PlayerIndex,GamePadState>();
        //private static Dictionary<PlayerIndex, Boolean> isMenuCooled = new Dictionary<PlayerIndex, Boolean>();
        //private static Dictionary<PlayerIndex, Boolean> isButtonCooled = new Dictionary<PlayerIndex, Boolean>();
        //private const int MENU_COOLDOWN = 1000;
        //private const int BUTTON_COOLDOWN = 1000;

        //private static Boolean isMenuOneCooled = true;
        //private static Boolean isButtonOneCooled = true;
        //private static Timer menuOneCooldownTimer;
        //private static Timer buttonOneCooldownTimer;

        //private static Boolean isMenuTwoCooled = true;
        //private static Boolean isButtonTwoCooled = true;
        //private static Timer menuTwoCooldownTimer;
        //private static Timer buttonTwoCooldownTimer;

        #endregion

        #region Properties

        //public static bool IsMenuOneCooled
        //{
        //    get
        //    {
        //        return isMenuOneCooled;
        //    }
        //    set
        //    {
        //        isMenuOneCooled = value;
        //        if (!value)
        //        {
        //            menuOneCooldownTimer.Start();
        //        }
        //    }
        //}

        //public static bool IsMenuTwoCooled
        //{
        //    get
        //    {
        //        return isMenuTwoCooled;
        //    }
        //    set
        //    {
        //        isMenuTwoCooled = value;
        //        if (!value)
        //        {
        //            menuTwoCooldownTimer.Start();
        //        }
        //    }
        //}

        //public static bool IsButtonOneCooled
        //{
        //    get
        //    {
        //        return isButtonOneCooled;
        //    }
        //    set
        //    {
        //        isButtonOneCooled = value;
        //        if (!value)
        //        {
        //            buttonOneCooldownTimer.Start();
        //        }
        //    }
        //}

        //public static bool IsButtonTwoCooled
        //{
        //    get
        //    {
        //        return isButtonTwoCooled;
        //    }
        //    set
        //    {
        //        isButtonTwoCooled = value;
        //        if (!value)
        //        {
        //            buttonTwoCooldownTimer.Start();
        //        }
        //    }
        //}

        #endregion

        #region Creators

        static InputHandler()
        {
            lastInputs.Add(PlayerIndex.One, GamePad.GetState(PlayerIndex.One));
            lastInputs.Add(PlayerIndex.Two, GamePad.GetState(PlayerIndex.Two));

            //isMenuCooled.Add(PlayerIndex.One, IsMenuOneCooled);
            //isMenuCooled.Add(PlayerIndex.Two, IsMenuTwoCooled);

            //isButtonCooled.Add(PlayerIndex.One, IsButtonOneCooled);
            //isButtonCooled.Add(PlayerIndex.Two, IsButtonTwoCooled);

            //menuOneCooldownTimer = new Timer(MENU_COOLDOWN);
            //menuOneCooldownTimer.Elapsed += new ElapsedEventHandler(OnMenuOneCooledEvent);

            //buttonOneCooldownTimer = new Timer(BUTTON_COOLDOWN);
            //buttonOneCooldownTimer.Elapsed += new ElapsedEventHandler(OnButtonOneCooledEvent);

            //menuTwoCooldownTimer = new Timer(MENU_COOLDOWN);
            //menuTwoCooldownTimer.Elapsed += new ElapsedEventHandler(OnMenuTwoCooledEvent);

            //buttonTwoCooldownTimer = new Timer(BUTTON_COOLDOWN);
            //buttonTwoCooldownTimer.Elapsed += new ElapsedEventHandler(OnButtonTwoCooledEvent);
        }

        #endregion

        #region Methods

        //private static void OnMenuOneCooledEvent(object source, ElapsedEventArgs e)
        //{
        //    IsMenuOneCooled = true;
        //}

        //private static void OnButtonOneCooledEvent(object source, ElapsedEventArgs e)
        //{
        //    IsButtonOneCooled = true;
        //}

        //private static void OnMenuTwoCooledEvent(object source, ElapsedEventArgs e)
        //{
        //    IsMenuTwoCooled = true;
        //}

        //private static void OnButtonTwoCooledEvent(object source, ElapsedEventArgs e)
        //{
        //    IsButtonTwoCooled = true;
        //}

        public static void Update()
        {
            lastInputs[PlayerIndex.One] = GamePad.GetState(PlayerIndex.One);
            lastInputs[PlayerIndex.Two] = GamePad.GetState(PlayerIndex.Two);
        }

        public static bool isConfirm(PlayerIndex player)
        {
            //if (GamePad.GetState(player).IsButtonDown(Buttons.A) && lastInputs[player].IsButtonUp(Buttons.A) && isButtonCooled[player])
            if (GamePad.GetState(player).IsButtonDown(Buttons.A) && lastInputs[player].IsButtonUp(Buttons.A) || Keyboard.GetState(player).IsKeyDown(Keys.Enter))
            {
                //if (player.Equals(PlayerIndex.One))
                //{
                //    IsButtonOneCooled = false;
                //}
                //else
                //{
                //    IsButtonTwoCooled = false;
                //}
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool isCancel(PlayerIndex player)
        {
            //if (GamePad.GetState(player).IsButtonDown(Buttons.B) && lastInputs[player].IsButtonUp(Buttons.B) && isButtonCooled[player])
            if (GamePad.GetState(player).IsButtonDown(Buttons.B) && lastInputs[player].IsButtonUp(Buttons.B))
            {
                //if (player.Equals(PlayerIndex.One))
                //{
                //    IsButtonOneCooled = false;
                //}
                //else
                //{
                //    IsButtonTwoCooled = false;
                //}
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool isMenuUp(PlayerIndex player)
        {
            if(GamePad.GetState(player).IsButtonDown(Buttons.DPadUp) && lastInputs[player].IsButtonUp(Buttons.DPadUp) || GamePad.GetState(player).IsButtonDown(Buttons.LeftThumbstickUp) && lastInputs[player].IsButtonUp(Buttons.LeftThumbstickUp))
            {
                //if (player.Equals(PlayerIndex.One))
                //{
                //    IsMenuOneCooled = false;
                //}
                //else
                //{
                //    IsMenuTwoCooled = false;
                //}
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool isMenuDown(PlayerIndex player)
        {
            if (GamePad.GetState(player).IsButtonDown(Buttons.DPadDown) && lastInputs[player].IsButtonUp(Buttons.DPadDown) || GamePad.GetState(player).IsButtonDown(Buttons.LeftThumbstickDown) && lastInputs[player].IsButtonUp(Buttons.LeftThumbstickDown))
            {
                //if (player.Equals(PlayerIndex.One))
                //{
                //    IsMenuOneCooled = false;
                //}
                //else
                //{
                //    IsMenuTwoCooled = false;
                //}
                return true;
            }
            else
            {
                return false;
            }
        }

        public static bool IsFire(PlayerIndex player)
        {
            //if (GamePad.GetState(player).IsButtonDown(Buttons.X) && lastInputs[player].IsButtonUp(Buttons.X) && isButtonCooled[player])
            if (GamePad.GetState(player).IsButtonDown(Buttons.X) && lastInputs[player].IsButtonUp(Buttons.X))
            {
                //if(player.Equals(PlayerIndex.One))
                //{
                //    IsButtonOneCooled = false;
                //}
                //else
                //{
                //    IsButtonTwoCooled = false;
                //}
                return true;
            }
            return false;
        }

        public static bool IsPause(PlayerIndex player)
        {            
            if (GamePad.GetState(player).IsButtonDown(Buttons.Start) && lastInputs[player].IsButtonUp(Buttons.Start))
            {
                return true;
            }            
            return false;
        }

        public static Vector2 Direction(PlayerIndex player)
        {
            Vector2 direction = GamePad.GetState(player).ThumbSticks.Left;
            direction.Y = -direction.Y;
            direction.Normalize();
            return direction;
        }

        #endregion
    }
}
